package com.luntan.utils;

import java.security.MessageDigest;

public class Encrypter {
    // md5�����㷨
    public static String md5(String s) {
	try {
	    MessageDigest algorithm = MessageDigest.getInstance("MD5");
	    algorithm.reset();
	    algorithm.update(s.getBytes("UTF-8"));
	    return toHexString(algorithm.digest());
	} catch (Exception e) {
	    throw new RuntimeException(e);
	}
    }

    // SHA1�����㷨
    public static String SHA(String s) {
	try {
	    MessageDigest algorithm = MessageDigest.getInstance("SHA");
	    algorithm.reset();
	    algorithm.update(s.getBytes("UTF-8"));
	    return toHexString(algorithm.digest());
	} catch (Exception e) {
	    return s;
	}
    }

    // ��������
    private static String toHexString(byte[] byteArray) {
	StringBuffer md5StrBuff = new StringBuffer();

	for (int i = 0; i < byteArray.length; i++) {
	    if (Integer.toHexString(0xFF & byteArray[i]).length() == 1)
		md5StrBuff.append("0").append(Integer.toHexString(0xFF & byteArray[i]));
	    else
		md5StrBuff.append(Integer.toHexString(0xFF & byteArray[i]));
	}
	return md5StrBuff.toString();
    }
}
